package com.fit.pages.user;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.annotations.AdminAccess;
import com.fit.annotations.AnonymousAccess;
import com.fit.annotations.ProfessorAccess;
import com.fit.annotations.StudentAccess;
import com.fit.dao.user.UserDAO;
import com.fit.entities.user.User;
import com.fit.model.MD5;
import com.fit.model.UserRole;
import com.fit.pages.ErrorPage;

@AdminAccess
@AnonymousAccess
@ProfessorAccess
@StudentAccess
public class Add {

	 @Property
	 @SessionState(create = false)
	 private User user;
	 @Inject
	 private UserDAO userDAO;
	 @InjectPage
	 private ErrorPage errorPage;
	 
	 @Persist
	 @Property
	 private String error;
	 
	 @Persist
	 @Property
	 private String confirmation;


	 void onPrepare() {
	  user = new User();
	 }

	 Object onSubmitFromCreateForm() {
	  error="";
	  confirmation="";
	  	   
	   user.setRole(UserRole.ADMIN);
	   
	   user.setPassword(MD5.md5(user.getPassword()));
	   userDAO.save(user);
	   confirmation = "User successfully added";
	   return Browse.class;
	  
	  
	  
	  
	 }
	 
	 @OnEvent(component = "ResetConfLink")
	 Object onResetConfLink() {
	  confirmation="";
	  return null;
	 }
	 
	 @OnEvent(component = "ResetErrorLink")
	 Object onResetErrorLink() {
	  error="";
	  return null;
	 }
	 
	 
	 
	}